﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKView;

namespace ModelChecking.ModelValidation.AdjoinFireZone
{
	public partial class SelectFireZoneFloorForm : System.Windows.Forms.Form
	{
		public List<int> SelectedViewIndexs()
		{
			List<int> list = new List<int>();
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				bool itemChecked = this.chkList.GetItemChecked(i);
				if (itemChecked)
				{
					list.Add(i);
				}
			}
			return list;
		}

		public SelectFireZoneFloorForm(List<ViewPlan> views, Document doc, bool bIsHorViewPlan)
		{
			this.m_lstViews = views;
			this.InitializeComponent();
			this.m_bIsHorViewPlan = bIsHorViewPlan;
			this.m_doc = doc;
		}

		private void btnSelAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				this.chkList.SetItemChecked(i, true);
			}
		}

		private void btnSelNone_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				this.chkList.SetItemChecked(i, false);
			}
		}

		private void btnSelContrary_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				bool value = !this.chkList.GetItemChecked(i);
				this.chkList.SetItemChecked(i, value);
			}
		}

		private void SelectFireZoneFloorForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			string workingLocation = Product.WorkingLocation;
			this.m_strXMLPath = workingLocation + this.m_strXMLName;
			this.m_xmlDoc = XMLOperating.LoadXml(this.m_strXMLPath, "Root");
			this.ReadLevelDataFromXml();
			bool flag = true;
			for (int i = 0; i < this.m_lstViews.Count; i++)
			{
				ViewPlan viewPlan = this.m_lstViews[i];
				bool value = false;
				if (this.m_bIsHorViewPlan)
				{
					if (this.m_lstHorLevelName.IndexOf(viewPlan.Name) >= 0)
					{
						value = true;
						if (flag)
						{
							flag = false;
						}
					}
				}
				else if (this.m_lstVerLevelName.IndexOf(viewPlan.Name) >= 0)
				{
					value = true;
					if (flag)
					{
						flag = false;
					}
				}
				this.chkList.Items.Add(viewPlan.Name);
				this.chkList.SetItemChecked(i, value);
			}
			if (flag)
			{
				for (int j = 0; j < this.chkList.Items.Count; j++)
				{
					this.chkList.SetItemChecked(j, true);
				}
			}
		}

		public static List<string> GetAllFloorNames(XmlDocument xmlDoc, string rootNode)
		{
			List<string> list = new List<string>();
			try
			{
				XmlNode xmlNode = xmlDoc.SelectSingleNode(rootNode);
				foreach (object obj in xmlNode.ChildNodes)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					XmlAttribute xmlAttribute = xmlNode2.Attributes["Name"];
					if (xmlAttribute != null)
					{
						list.Add(xmlAttribute.Value);
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
			return list;
		}

		private bool ReadLevelDataFromXml()
		{
			try
			{
				this.m_lstHorLevelName = SelectFireZoneFloorForm.GetAllFloorNames(this.m_xmlDoc, "Root/HorLevel");
				this.m_lstVerLevelName = SelectFireZoneFloorForm.GetAllFloorNames(this.m_xmlDoc, "Root/VerLevel");
			}
			catch
			{
				return false;
			}
			return true;
		}

		private void WriteLevelData2Xml()
		{
			XmlNode xmlNode = this.m_xmlDoc.CreateNode(XmlNodeType.Element, "Root", "");
			XmlElement xmlElement = this.m_xmlDoc.CreateElement("HorLevel");
			XmlElement xmlElement2 = this.m_xmlDoc.CreateElement("VerLevel");
			string name = "FloorName";
			foreach (string value in this.m_lstHorLevelName)
			{
				XmlElement xmlElement3 = this.m_xmlDoc.CreateElement(name);
				xmlElement3.SetAttribute("Name", value);
				xmlElement.AppendChild(xmlElement3);
			}
			foreach (string value2 in this.m_lstVerLevelName)
			{
				XmlElement xmlElement4 = this.m_xmlDoc.CreateElement(name);
				xmlElement4.SetAttribute("Name", value2);
				xmlElement2.AppendChild(xmlElement4);
			}
			xmlNode.AppendChild(xmlElement);
			xmlNode.AppendChild(xmlElement2);
			this.m_xmlDoc.ReplaceChild(xmlNode, this.m_xmlDoc.SelectSingleNode("Root"));
			this.m_xmlDoc.Save(this.m_strXMLPath);
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			List<int> list = this.SelectedViewIndexs();
			if (this.m_bIsHorViewPlan)
			{
				this.m_lstHorLevelName.Clear();
				foreach (int index in list)
				{
					this.m_lstHorLevelName.Add(this.m_lstViews[index].Name);
				}
				if (this.m_lstHorLevelName.Count == 0)
				{
					base.DialogResult = DialogResult.Cancel;
					return;
				}
			}
			else
			{
				if (!this.GetVerCheckLevel())
				{
					base.DialogResult = DialogResult.Cancel;
					return;
				}
				this.m_lstVerLevelName.Clear();
				foreach (int index2 in list)
				{
					this.m_lstVerLevelName.Add(this.m_lstViews[index2].Name);
				}
			}
			this.WriteLevelData2Xml();
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private bool GetVerCheckLevel()
		{
			List<ViewPlan> list = new List<ViewPlan>();
			foreach (int index in this.SelectedViewIndexs())
			{
				list.Add(this.m_lstViews[index]);
			}
			if (list.Count < 2)
			{
				YJKMessageBox.Information("请您至少选择相邻的两层！");
				return false;
			}
			Dictionary<ViewPlan, ViewPlan> dictionary = new Dictionary<ViewPlan, ViewPlan>();
			List<Level> sortLevels = FloorSelectManager.GetSortLevels(this.m_doc, double.MinValue, double.MaxValue);
			for (int i = 0; i < list.Count - 1; i++)
			{
				ViewPlan viewPlan = list[i];
				ViewPlan viewPlan2 = list[i + 1];
				int num = -1;
				int num2 = -1;
				for (int j = 0; j < sortLevels.Count; j++)
				{
					Level level = sortLevels[j];
					if (num < 0 && level.Id == viewPlan.GenLevel.Id)
					{
						num = j;
					}
					if (num2 < 0 && level.Id == viewPlan2.GenLevel.Id)
					{
						num2 = j;
					}
				}
				if (num2 - num == 1)
				{
					dictionary.Add(viewPlan, viewPlan2);
				}
			}
			if (dictionary.Count == 0)
			{
				YJKMessageBox.Information("请您选择相邻的楼层！");
				return false;
			}
			this.m_dictPairPlan = dictionary;
			return true;
		}

		private List<ViewPlan> m_lstViews = new List<ViewPlan>();

		private string m_strXMLName = "AdjoinFireZoneDoorWinCheck.xml";

		private string m_strXMLPath;

		private XmlDocument m_xmlDoc;

		private List<string> m_lstHorLevelName = new List<string>();

		private List<string> m_lstVerLevelName = new List<string>();

		private bool m_bIsHorViewPlan = true;

		private Document m_doc;

		public Dictionary<ViewPlan, ViewPlan> m_dictPairPlan = new Dictionary<ViewPlan, ViewPlan>();
	}
}
